<div class="container" style="min-height: 206px;">
<div class="heading"><a id="sidebaropengear" onclick="showSideBarSettings();" href="javascript:void(0);"><img width=16"
    height="16" src="./images/gear.png" alt="" /></a>Information
</div>
<div id="selectedcustomernav" class="inside" style="padding-bottom:0px !important;">
    <input type="text" id="remote-users" style="width:143px !important;display:none"/>

    <div class="selectedcustomer" style="text-align:center;">
        <center><img src="<?=getImage($location->customer->image,'images/content/',75,75);?>"/></center>
        <span id="selectedcustomer"><a href="index.php?r=Customers/update&id=<?=$location->customer->custno?>"><?php echo $location->customer->lastname?></a></span>

    </div>


</div>

<div class="inside" style="padding-top:0px; padding-bottom:0px;">
<h5 style="padding-left:11px;">Operations</h5>
<ul class="links" style="margin-left:0px !important;">
<li id='blankforvalidation2' style="display:none">&nbsp;</li>
<script>
itemArray = new Array();
empArray = new Array();
Ext.onReady(function(){
 vendorsItemsStore.each(function (item) {
    itemArray.push(item.data);
});

techStore.each(function (item) {
    empArray.push(item.data);
});   
});


function printSelect(option){
    var printPanel;
    if (!printPanel) {
        printForm = Ext.create('Ext.form.Panel', {
            action:'submit',
            defaults:{
                anchor:'100%'
            },
            bodyPadding:10,
            items:[

                {
                    xtype: 'label',

                    text: 'Select Print Type',
                    margins: '0 0 0 10'
                }
            ]
        });
        printPanel = Ext.create('Ext.window.Window', {
            title:'Customer Connect',
            width:100,
            height:50,
            layout:'fit',
            items:printForm,
            closeAction:'hide',
            buttons:[
                {
                    text:'Total Cost',
                    handler:function () {
                        var openlink=window.open('','_blank');
                        openlink.location=option.link + '&total=1';
                        printPanel.hide();

                    }
                },
                {
                    text:'Itemized',
                    handler:function () {
                        var openlink=window.open('','_blank');
                        openlink.location=option.link;
                        printPanel.hide();
                    }
                }
            ]
        });
    }
    printPanel.show();
    printPanel.center();
}
function saveData(callback) {

    $.ajax({
        url:'index.php?r=invoiceLedger/saveBatch',
        type:'post',
        data:{
            invoice:invoice,
            data:itemArray
        },
        timeOut:3000,
        success:function () {
            
            itemArray = new Array();

            $.ajax({
                url:'index.php?r=invoiceEmp/saveBatch',
                type:'post',
                data:{
                    invoice:invoice,
                    data:empArray
                },
                timeOut:3000,
                success:function () {
                    empArray = new Array();
                    callback();
                }
            });
        }
    });

}
function saveForm(callback) {

    var submitData = $('#invoice-form').serializeArray();
    var i = 0;
    for (i = 0; i < submitData.length; i++) {
        if (submitData[i].name.slice(0, 8) != 'Invoice[') {
            
            submitData.remove(i);
        }
    }
    submitData = $.param(submitData);
    $.ajax({
        url:'index.php?r=invoices/update&invoice=' + invoice,
        type:'post',
        data:submitData,
        timeOut:3000,
        success:function (data) {

            if (data == 'true') {

                callback();
            } else {
                console.log(data);
                $('.errorMessage').remove();
                data = $.parseJSON(data);
                errorList = new Array();
                for (var p in data) {

                    var error = '';
                    var i = 0;
                    error += data[p];
                    console.log(p);
                    errorList.push(error);
                    $('<div class="errorMessage">' + error + '</div>').insertAfter($('#' + p));

                }

                $('input, select, textarea').change(function () {
                    $(this).next('.errorMessage').remove();
                });

            }
        }
    });
    return false;
}
function save(option) {
    showOkDialog('Please wait while saving');   
    saveData(function () {
       
        saveForm(function () {
            Ext.WindowManager.hideAll()
            if (option!=null && option.link != null) {
                if(option!=null && option.print != null) {
                    printSelect(option);
                } else
                if(option.newtab!=null && option.newtab==true){
                    var openlink=window.open('','_blank');
                    openlink.location=option.link;

                }else{
                    window.location = option.link;
                }

            } else {
                //showOkDialog('Save successful');
            }

        });

    });

}

function createSalesOrders() {

    saveForm(function () {
        $.ajax({
            url:'index.php?r=invoices/checkQuoteApproved',
            data:{
                quote:invoice
            },
            type:'get',
            success:function (rs) {
                if (rs == 'true') {
                    saveData(function () {
                        window.location = 'index.php?r=invoices/createSalesOrders&quoteinvoice='+invoice;
                    });

                } else {
                    Ext.MessageBox.show({
                        title:'Customer Connect',
                        msg:'Quote need to approve before create Sales Order',
                        buttons:Ext.Msg.OK
                    });
                }
            }
        })

    });

}
function createInvoiceWithoutSave(salesOrderId) {

    
            $.ajax({
                url:'index.php?r=invoices/isInvoiceCreatable',
                data:{
                    id:salesOrderId
                },
                type:'get',
                success:function(rs){
                    if(rs==''){
                        window.location = 'index.php?r=invoices/createInvoice&saleinvoice='+invoice;
                    }else{
                        showOkDialog(rs);
                    }
                }
            })

    //if(link!=null) window.location=link;
}
function createInvoice(salesOrderId) {

    saveData(function () {
        saveForm(function () {
            $.ajax({
                url:'index.php?r=invoices/isInvoiceCreatable',
                data:{
                    id:salesOrderId
                },
                type:'get',
                success:function(rs){
                    if(rs==''){
                        window.location = 'index.php?r=invoices/createInvoice&saleinvoice='+invoice;
                    }else{
                        showOkDialog(rs);
                    }
                }
            })

        });

    })
    //if(link!=null) window.location=link;
}
</script>

<?php if ($model->agreementno != NULL): ?>
    <li><a class="return-button" href="<?php echo $this->createUrl('/contracts/update',array('contractid'=>$model->agreementno)); ?>">Back to Contract</a></li>
    <?php endif; ?>
    <?php if ($model->jobnumber != NULL): ?>
    <li><a class="return-button"  href="<?php echo $this->createUrl('/jobs/update',array('jobid'=>$model->jobnumber)); ?>">Back to Job</a></li>
    <?php endif; ?>
    
    <li><a class="return-button" href="<?php echo $this->createUrl('/locations/update',array('custno'=>$model->custno,'locno'=>$model->locno)); ?>">Back to Location</a></li>
    
    
<?php if ($type == Invoice::TYPE_QUOTE): ?>
    <?php $this->renderPartial('_quoteMenu',array(
        'model'=>$model,
        'action'=>$action
    ));?>
    <?php elseif ($type == Invoice::TYPE_SALES_ORDERS): ?>
    <?php $this->renderPartial('_salesOrderMenu',array(
        'model'=>$model,
        'action'=>$action
    ));?>
    <?php elseif ($type == Invoice::TYPE_INVOICE): ?>

    <?php $this->renderPartial('_invoiceMenu',array(
        'model'=>$model,
        'action'=>$action
    ));?>
    <?php elseif($type==Invoice::TYPE_CREDIT_MEMO): ?>
    <?php $this->renderPartial('_creditMemoMenu',array(
        'model'=>$model,
        'action'=>$action
    ));?>
    <?php endif ?>
</ul>
</div>


</div>

<?php if ($type == Invoice::TYPE_INVOICE): ?>
 
<?php
$criteria = new CDbCriteria();
$criteria->condition = "invoiceNo='$model->invoice'";
$criteria->order = "date Asc";
$paymentsApplied = QbPaymentsapplied::model()->findAll($criteria);
if($paymentsApplied):
?>
<div class="container">
    <div class="heading">Payments Applied</div>
    <div class="inside">
        <ul>
    <?php foreach ($paymentsApplied as $payment): ?>
    <li><?=$payment->date?> -  $<?=$payment->invoiceAmount ?></li>
    <?php endforeach; ?>
  
    </ul></div>
</div>    
<?php endif; ?>

<?php endif; ?>
